<?php
namespace Tbf\Component\Http;
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;

use Guzzle\Http\Message\Request as GuzzleRequest;
use Guzzle\Http\Message\EntityEnclosingRequest as GuzzleEntityEnclosingRequest;
use Guzzle\Http\Message\Response as GuzzleResponse;
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
use Tbf\Component\Http\Adapter\GuzzleToSymfonyRequest;

class Adapter
{
    /**
     * transform guzzle request to symfony request
     * @param  GuzzleRequest  $guzzle_request
     * @return SymfonyRequest
     */
    public static function guzzleToSymfonyRequest(GuzzleRequest $guzzle_request)
    {
        $obj = new GuzzleToSymfonyRequest();

        return $obj->handle($guzzle_request);
    }

    /**
     * transform symfony response to guzzle response
     * @param  SymfonyResponse $symfony_response
     * @return GuzzleResponse
     */
    public static function symfonyToGuzzleResponse(SymfonyResponse $symfony_response)
    {
        $status = $symfony_response->getStatusCode();
        $headers = $symfony_response->headers->all();
        $body = $symfony_response->getContent();

        $guzzle_response = new GuzzleResponse($status, $headers, $body);

        return $guzzle_response;
    }

    /**
     * transform symfony request to guzzle request
     * @param  SymfonyRequest $symfony_request
     * @return GuzzleRequest
     */
    public static function symfonyToGuzzleRequest(SymfonyRequest $symfony_request)
    {
        $method = $symfony_request->getMethod();
        $url = $symfony_request->getUri();
        $header_map = $symfony_request->headers->all();
        $guzzle_request = new GuzzleEntityEnclosingRequest($method, $url, $header_map);

        return $guzzle_request;
    }

    public static function guzzleToSymfonyResponse(GuzzleResponse $guzzle_response)
    {
        $content = $guzzle_response->getBody(true);
        $status = $guzzle_response->getStatusCode();
        $headers = $guzzle_response->getHeaders(false);
        $symfony_response = new SymfonyResponse($content, $status, $headers);

        return $symfony_response;
    }

    /**
     * 将symfony的request的里面的信息放到php全局变量里面
     * @param SymfonyRequest $request
     */
    public static function dumpSymfonyRequestToPhpGlobal(SymfonyRequest $request)
    {
        $_POST = $request->request->all();
        $_GET = $request->query->all();
        $_COOKIE = $request->cookies->all();
    }
}
